package dbutil

import (
	"BingWallpaper/entity"
	"BingWallpaper/logger"
	"fmt"

	"database/sql"

	_ "github.com/mattn/go-sqlite3"
)

//添加记录
func Add(bing entity.Bing, imgFileName string) {
	if recordExists(bing) {
		logger.Loginfo(fmt.Sprintf("记录已存在:%s ", imgFileName))
	} else {

		db, err := sql.Open("sqlite3", "./Database/bing.db")
		if err != nil {
			logger.Loginfo(err.Error())
		}

		stmt, err := db.Prepare("INSERT INTO detail(hsh,filename,filedesc,enddate) values(?,?,?,?)")
		if err != nil {
			logger.Loginfo(err.Error())
		}

		res, err := stmt.Exec(bing.Images[0].Hsh, imgFileName, bing.Images[0].Copyright, bing.Images[0].Enddate)
		if err != nil {
			logger.Loginfo(err.Error())
		}

		res.LastInsertId()
		// id, err := res.LastInsertId()
		// if err != nil {
		// 	logger.Loginfo(err.Error())
		// }

		//logger.Loginfo(strconv.FormatInt(int64, id) + "=>数据入库成功")

		db.Close()
	}
}
